﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SQLEditor.admin.entity;
using SQLEditor.admin.service;
using SQLEditor.dal;

namespace SQLEditor.admin.ashx
{
    /// <summary>
    /// rm_sqlquestion 的摘要说明
    /// </summary>
    public class rm_sqlquestion : rm_base
    {
        SqlQuestionService bs = new SqlQuestionService();
        public override void getall(HttpContext context)
        {
            string json = bs.getall();
            context.Response.Write(json);
        }

        public override void search(HttpContext context)
        {
            string strWhere = "1=1";
            string sort = context.Request.Params["sort"] ?? "id";   //排序列
            string order = context.Request.Params["order"] ?? "asc";     //排序方式 asc或者desc
            int pageIndex = int.Parse(context.Request.Params["page"] ?? "1");
            int pageSize = int.Parse(context.Request.Params["rows"] ?? "20");

            string json = bs.search(pageSize, pageIndex, sort + " " + order, strWhere);
            context.Response.Write(json);

        }

        public override void add(HttpContext context)
        {
            string title = context.Request.Params["title"] ?? "";
            string answer = context.Request.Params["answer"] ?? "";
            string explain = context.Request.Params["explain"] ?? "";
            string point = context.Request.Params["point"] ?? "";
            string projectId = context.Request.Params["projectId"] ?? "";
            int userId = UserService.GetUser(context).id;
            sqlQuestion b = new sqlQuestion
            {
                title  =title,
                answer = answer,
                explain = explain,
                point = int.Parse( point),
                projectId = int.Parse( projectId),
                userId = userId,
                addDate = DateTime.Now
            };
            string json = bs.add(b);
            context.Response.Write(json);

        }
        public override void edit(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request.Params["id"] ?? "0");
            string title = context.Request.Params["title"] ?? "";
            string answer = context.Request.Params["answer"] ?? "";
            string explain = context.Request.Params["explain"] ?? "";
            string point = context.Request.Params["point"] ?? "";
            string projectId = context.Request.Params["projectId"] ?? "";
            int userId = UserService.GetUser(context).id;
            sqlQuestion b = new sqlQuestion
            {
                id = id,
                title = title,
                answer = answer,
                explain = explain,
                point = int.Parse(point),
                projectId = int.Parse(projectId),
            };
            string json = bs.edit(b);
            context.Response.Write(json);
        }

        public override void delete(HttpContext context)
        {
            string ids = context.Request.Params["ids"];
            string json = bs.delete(ids);
            context.Response.Write(json);
        }
        public override void other(HttpContext context)
        {
            string action = context.Request.Params["action"];
            UserEntity user = UserService.GetUser(context);   //获取cookie里的用户对象
            switch (action) {                
                case "import":
                    {
                        //文件名  
                        HttpPostedFile f1 = context.Request.Files["ui_sqlquestion_import_file"];
                           
                        string fileExt = System.IO.Path.GetExtension(f1.FileName);
                        if (!(fileExt == ".txt" || fileExt == ".TXT"))
                        {
                            context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "只支持扩展名为.txt或.TXT文件！" }));
                            return;
                        }
                        try
                        {
                            bs.import(f1.InputStream, user.id);
                            context.Response.Write(JsonUtil.ToJson(new { success = true, msg = "导入成功！" }));
                        }
                        catch (Exception ex)
                        {
                            context.Response.Write(JsonUtil.ToJson(new { success = false, msg = ex.Message }));
                        }
                        break;
                    }
            }
        }
    }
}